<!--
// ==============================================================================================
// Licensed under the WEBO license (LICENSE.txt)
// ==============================================================================================
// @author     WEBO Software (http://www.webogroup.com/)
// @version    0.2.0
// @copyright  Copyright &copy; 2009-2010 WEBO Software, All Rights Reserved
// ==============================================================================================
// To build WEBO Site InSight packages just setup settings on config.js and open this file in IE.
// ==============================================================================================
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru"><head><title>WEBO Site InSight Build System</title></head>
<body>
<form action="" method="get" onsubmit="build();return false;">
<p>Build: <input name="number" id="number" value="0.1.0" size="10"/><input type="submit" value="Cast me!"/></p>
<p>General log:<br/><textarea id="logmain" rows="20" cols="120"></textarea></p>
<p>Detailed log:<br/><textarea id="logtxt" rows="10" cols="120"></textarea></p>
</form>
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript" src="builds.js"></script>
<script type="text/javascript" src="libs.js"></script>
<script type="text/javascript">
/* build number */
var	n;
function build() {
	var build,
		file, b, rule, str, b_dst,
		files_list, rules_list, dst_file,
		fso,
		src, dst, builddir, folder, folders,
		j, i,
		count = 0,
		wsh;
		n = document.getElementById('number').value;
	document.getElementById('logmain').value = 
	document.getElementById('logtxt').value = '';
	try {
		i = 0;
/* loop in required builds and find list of files */
		while (build = builds[i++]) {
			b = build["file"];
			b_dst = build_filename(b);
/* new ActiveX object to work with files */
			fso = new ActiveXObject("Scripting.FileSystemObject");
			log('Clearing folder for "' + b + '"', 2);
			builddir = buildRoot + "!" + " " + "builds\\" + b + (b === 'wordpress' ? "\\webo-site-insight" : '');
			if (fso.FileExists(buildRoot + "!" + " " + "builds\\" + b_dst)) {
				fso.DeleteFile(buildRoot + "!" + " " + "builds\\" + b_dst , true);
			}
			log('Opening files list for "' + b + '"', 2);
/* get list of files */
			if (fso.FileExists(buildRoot + b + "\\files.stable")) {
				files_list = fso.OpenTextFile(buildRoot + b + "\\files.stable");
			} else {
				files_list = fso.OpenTextFile(buildRoot + b + "\\files");
			}
			try {
/* create files from list */
				while (!files_list.AtEndOfStream && (file = files_list.ReadLine())) {
/* build plugins from the core folder */
					if (!file.indexOf('trunk')) {
						src = buildRoot + "\\" + file.replace(/\//g, "\\");
					} else {
						src = buildRoot + "\\" + b + "\\" + file.replace(/\//g, "\\");
					}
					dst = builddir + "\\" + file.replace(/trunk\//,'').replace(/\//g, "\\");
					folder = dst.substr(0, dst.length - 1);
					if (src.substr(src.length - 1, 1) == '\\' && !fso.FolderExists(folder)) {
						log('Creating folder "' + folder + '"', 2);
						fso.CreateFolder(dst);
					} else {
						folder = dst.replace(/[^\\]+$/, "");
						if (!fso.FolderExists(folder)) {
							folders = folder.split('\\');
							j = 0;
							folder = '';
							while (folders[j++]) {
								folder += folders[j-1];
								if (!fso.FolderExists(folder)) {
									log('Creating folder "' + folder + '"', 2);
									fso.CreateFolder(folder);
								}
								folder += "\\";
							}
						}
						log('Creating file "' + dst + '"', 2);
						fso.CopyFile(src, dst);
						count++;
					}
				}
			} catch (e) {
				if (file) {
					log("ERROR: in files list " + e.name + ": " + e.message + "(" + e.description + ')' + (file ? " " + file : ""), 1);
				}
			}
			files_list.Close();
/* create files from rules */
			try {
				j = 0;
				if (build["rules"]) {
					while (rule = build["rules"][j++]) {
						dst = buildRoot + "!" + " " + "builds\\" + b + "\\" + rule["dst"].replace(/\//g, "\\");
						if (!rule["src"]) {
							if (!fso.FolderExists(dst)) {
								fso.CreateFolder(dst);
							}
						} else {
							src = buildRoot + rule["src"].replace(/\//g, "\\");
							switch (rule["type"]) {
								case 'include_files':
									rules_list = fso.OpenTextFile(src);
									dst_file = fso.OpenTextFile(dst, 2, true);
									str = '';
									while (!rules_list.AtEndOfStream) {
										str += rules_list.ReadLine() + "\n";
									}
									dst_file.Write(window[rule["type"]](str, src.replace(/[^\\]+$/,'')));
									dst_file.Close();
									break;
								case 'copy_raw':
									fso.CopyFile(src, dst);
									break;
								default:
									dst_file = fso.CreateTextFile(dst, true);
									rules_list = fso.OpenTextFile(src);
/* get required file content and parse it */
									while (!rules_list.AtEndOfStream) {
										if (str = rules_list.ReadLine()) {
/* skip comments for localization */
											if (rule["type"].indexOf("lang") || (!rule["type"].indexOf("lang") && str.indexOf("/*") && str.indexOf(" *"))) {
												dst_file.WriteLine(window[rule["type"]](str));
											}
										}
									}
									break;
							}
							dst_file.Close();
							count++;
						}
					}
				}
			} catch (e) {
				log("ERROR: in rules " + e.name + ": " + e.message + "(" + e.description + ')' + " " + dst, 1);
			}
			log('Building ' + b_dst + ", total files: " + count, 1);
			log("=======================================", 2);
			count = 0;
/* add to gzip archive */
			try {
				wsh = new ActiveXObject("WScript.Shell");
				switch (b) {
					case 'joomla':
					case 'wordpress':
						folder = buildRoot + "!" + " " + "builds\\" + b + "\\*";
						break;
					default:
						folder = builddir + "\\*";
						break;
				}
				log("Creating ZIP from " + b + ", build " + n, 2);
				log("=======================================", 2);
				wsh.run("\"" + build7z + "\" a -tzip -mx9 \"" + buildRoot + "!" + " " + "builds\\" + b_dst + "\" \"" + folder +"\"");
			} catch (e) {
				log("ERROR: in GZIP " + e.name + ":" + e.message + "(" + e.description + ')', 1);
			}
		}
	} catch (e) {
		log("ERROR: general " + e.name + ": " + e.message + "(" + e.description + ')' + ", " + buildRoot + b, 1);
	}
}
</script>
</body>
</html>